package top.jacktgq.web.servlet;
/**
 * @Author CandyWall
 * @Date 2021/6/20--12:33
 * @Description 处理学生信息的servlet
 */

import top.jacktgq.dao.StudentDao;
import top.jacktgq.pojo.Student;
import top.jacktgq.utils.MapperUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "GetStudentByIdServlet", value = "/getStudentById")
public class GetStudentByIdServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String stu_id = request.getParameter("stu_id");
        StudentDao studentDao = new StudentDao();
        Student student = null;
        try {
            student = studentDao.getStudentById(stu_id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        response.setContentType("charset=utf-8;json/html");
        response.setCharacterEncoding("utf-8");
        try {
            response.getWriter().write(MapperUtils.obj2json(student));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
